import re

from loguru import logger
import execjs
import requests

url = 'https://www.mafengwo.cn/'

ss = requests.Session()
ss.headers = {
    "Host": "www.mafengwo.cn",
    "Connection": "keep-alive",
    "Accept": "application/json, text/javascript, */*; q=0.01",
    "X-Requested-With": "XMLHttpRequest",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
    "Referer": "https://www.mafengwo.cn/",
    "Accept-Encoding": "gzip, deflate, br",
    "Accept-Language": "zh-CN,zh;q=0.9"
}

res = ss.get(url)
logger.info(f'{res.status_code}')
first_data = res.text
# '__jsl_clearance_s=1705794821.221|-1|xSLj%2FAn1si7Q3Ar%2BSPOXkwNyV0c%3D; Max-age=3600; Path=/; SameSite=None; Secure'
code = re.findall(r'cookie=(.*?);location', first_data)[0]
cookie_str = execjs.eval(code)

cookie_jsl_clearance_s = cookie_str.split('=')[1].split(';')[0]
logger.info(f'1.生成cookie: cookie_jsl_clearance_s: {cookie_jsl_clearance_s}', )
ss.cookies['__jsl_clearance_s'] = cookie_jsl_clearance_s

# 2. 第二次请求
code = '''
window = globalThis;
navigator = {
    'userAgent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
}
document = {cookie: ''}
location = {
    "ancestorOrigins": {},
    "href": "https://www.mafengwo.cn/",
    "origin": "https://www.mafengwo.cn",
    "protocol": "https:",
    "host": "www.mafengwo.cn",
    "hostname": "www.mafengwo.cn",
    "port": "",
    "pathname": "/",
    "search": "",
    "hash": ""
};
setTimeout = function(fun, t){fun()};
'''
res = ss.get(url)
logger.info(f'{res.status_code}')
data2 = res.text
script_code = re.findall(r'<script[^>]*>([\s\S]*?)</script>', data2)[0]
code += script_code
code += '; function getCookie(){return document.cookie}'
# print(code)
cookie_str = execjs.compile(code).call('getCookie')
logger.info(f'2.生成cookie:{cookie_str}\n')
ss.cookies['__jsl_clearance_s'] = cookie_str.split('=', maxsplit=1)[1].split(';')[0]
# print('cookie', ss.cookies.get_dict(), )

# 第三次请求
# url = 'https://www.mafengwo.cn/flight/cityairportapi'
res = ss.get(url)
# data = res.content.decode('utf-8')
logger.info(f'{res.status_code}')
print(res.text)
